﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine.UI;

namespace Miao.UI
{
    /// <summary>
    /// 需要过度效果的Panel
    /// </summary>
    public abstract class UIMotionPanel : UIPanel
    {
        public bool closeOrHide = true;
        /// <summary>
        /// 还可以使用ImageButton
        /// </summary>
        public Button closeButton;
        public IMotion ShowMotion
        {
            get
            {
                return showMotion;
            }
            set
            {
                showMotion = value;
                showMotion.TweenGameObject = this.gameObject;
                showMotion.Init();
            }
        }
        protected IMotion showMotion;
        public IMotion HideMotion
        {
            get
            {
                return hideMotion;
            }
            set
            {
                hideMotion = value;
                hideMotion.TweenGameObject = this.gameObject;
                hideMotion.Init();
            }
        }
        protected IMotion hideMotion;
        public override async Task OnCreate()
        {
            if (closeOrHide)
            {
                closeButton?.onClick.AddListener(Close);
            }
            else
            {
                closeButton?.onClick.AddListener(Hide);
            }
            await base.OnCreate();
        }

        protected virtual void OnImmediatelyShow()
        {

        }
        protected virtual void OnImmediatelyHideClose()
        {

        }
        /// <summary>
        /// <para>等待动画完成再执行</para>
        /// <para>可读性决定性优势：你的 ShowAsync 方法用异步写法非常清晰，如果改成回调会变成回调地狱</para>
        /// <para>维护性：异步代码更容易调试、修改和扩展</para>
        /// <para>错误处理：async/await 可以用 try-catch，回调方式的错误处理会很复杂</para>
        /// <para>性能影响微乎其微：UI 动画不是高频操作，TaskCompletionSource 的开销完全可以接受</para>
        /// <para>团队协作：异步代码更容易被其他开发者理解</para>
        /// <para>TaskCompletionSource 方式相比 complete 大约有 5-10% 的性能开销</para>
        /// </summary>
        public async Task ShowAsync()
        {
            OnImmediatelyShow();
            if (showMotion != null)
            {
                gameObject.SetActive(true);

                // 创建TaskCompletionSource来等待动画完成
                var tcs = new TaskCompletionSource<bool>();

                ShowMotion.Play(true, () => {
                    OnShow();
                    tcs.SetResult(true);
                });

                await tcs.Task;
            }
            else
            {
                base.Show();
            }
        }

        public override void Show()
        {
            OnImmediatelyShow();
            if (showMotion != null)
            {
                gameObject.SetActive(true);
                ShowMotion.Play(true, OnShow);
            }
            else
            {
                base.Show();
            }
        }



        public override void Hide()
        {
            OnImmediatelyHideClose();
            if (hideMotion!=null)
            {
                HideMotion.Play(false, base.Hide);
            }
            else
            {
                base.Hide();
            }
        }

        public async Task HideAsync()
        {
            OnImmediatelyHideClose();
            if (hideMotion != null)
            {
                // 创建TaskCompletionSource来等待动画完成
                var tcs = new TaskCompletionSource<bool>();

                HideMotion.Play(false, () => {
                    base.Hide();
                    tcs.SetResult(true);
                });

                await tcs.Task;
            }
            else
            {
                base.Hide();
            }
        }
        public override void Close()
        {
            OnImmediatelyHideClose();
            if (hideMotion != null)
            {
                HideMotion.Play(false, base.Close);
            }
            else
            {
                base.Close();
            }
        }
        public async Task CloseAsync()
        {
            OnImmediatelyHideClose();
            if (hideMotion != null)
            {
                // 创建TaskCompletionSource来等待动画完成
                var tcs = new TaskCompletionSource<bool>();

                HideMotion.Play(false, () => {
                    base.Close();
                    tcs.SetResult(true);
                });

                await tcs.Task;
            }
            else
            {
                base.Close();
            }
        }

    }
}
